﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Script.Serialization;

namespace ALinq.BBS.DataAccess
{
    partial class Forum
    {
        Forum forum;

        partial void OnCreated()
        {
            this.forum = this;
        }

        private Posts posts;
        private Post lastPost;

        [ScriptIgnore]
        public Posts Posts
        {
            get
            {
                if (posts == null)
                    posts = new Posts(this);

                return posts;
            }
        }

        [ScriptIgnore]
        public Post LastPost
        {
            get
            {
                if (lastPost == null && LastPostID != null)
                {
                    lastPost = Posts.SingleOrDefault(o => o.ID == LastPostID);
                    if (lastPost == null)
                        LastPostID = null;
                }
                return lastPost;
            }
        }

        public void AddItem(Post post)
        {
            post.Forum = forum;
            //DataContext.Posts.InsertOnSubmit(post);
            AddPost(post.Title, post.Content, post.UserName);
        }

        public void DeletePost(int postID)
        {
            DataContext.Connection.Open();
            DataContext.Transaction = DataContext.Connection.BeginTransaction();
            try
            {
                var post = Posts.FindItems(o => o.ID == postID).SingleOrDefault();
                if (post == null)
                    return;

                DataContext.Posts.Delete(o => o.ID == postID);
                DataContext.Forums.Update(o => new Forum
                                                {
                                                    PostsCount = o.PostsCount - 1,
                                                    RepliesCount = o.RepliesCount - post.RepliesCount
                                                },
                                          o => o.ID == post.ForumID);
                DataContext.Users.Update(o => new User { PostsCount = o.PostsCount - 1 },
                                         o => o.ID == post.UserID);

                DataContext.Replies.Delete(o => o.PostID == postID);

                DataContext.Transaction.Commit();

                forum.PostsCount = forum.PostsCount - 1;
            }
            catch (Exception exc)
            {
                DataContext.Transaction.Rollback();
                throw;
            }
            finally
            {
                DataContext.Connection.Close();
            }
        }

        public void UpdatePost(int postID, string title, string content, string updateBy)
        {
            //using (var db = new Database())
            //{
            var db = DataContext;
            db.Connection.Open();
            db.Transaction = db.Connection.BeginTransaction();

            try
            {
                db.Posts.Update(o => new Post
                                    {
                                        ID = postID,
                                        Title = title,
                                        Content = content,
                                        LastUpdate = DateTime.Now,
                                        UpdateBy = updateBy,
                                    },
                                     o => o.ID == postID);

                db.Transaction.Commit();
            }
            catch (Exception exc)
            {
                db.Transaction.Rollback();
                throw exc;
            }

        }

        public Post AddPost(string title, string content, string userName)
        {
            using (var dc = new Database())
            {
                dc.Connection.Open();

                var userID = dc.Users.Where(o => o.Name == userName).Select(o => o.ID).Single();
                var post = new Post
                {
                    ID = dc.Posts.Max(o => o.ID) + 1,
                    Title = title,
                    Content = content,
                    UserName = userName,
                    UserID = userID,
                    ForumID = forum.ID
                };

                var tran = dc.Connection.BeginTransaction();
                try
                {
                    dc.Posts.Insert(post);
                    dc.Forums.Update(o => new Forum { PostsCount = o.PostsCount + 1, LastPostID = post.ID },
                                     o => o.ID == forum.ID);
                    dc.Users.Update(o => new User { PostsCount = o.PostsCount + 1 },
                                    o => o.ID == userID);
                    tran.Commit();

                    return post;
                }
                catch (Exception exc)
                {
                    tran.Rollback();
                    throw;
                }
            }

        }

    }
}